const leader = {};

var common = require("common.js").common;

//团长信息
leader.getMyLeaderInfo = ((openid, leader_id_share) => {
    var openid = openid ? openid : wx.getStorageSync("user").openid;
    //用户数据
        common.request({
        endpoint: "leader/myleaderinfo",
        method: "post",
        data: {
            openid: openid
        },
        success: res => {
            const page = getCurrentPages().pop();
            var leaderA = [];
            if (res.extra.location) {
                leaderA = res.extra.location.split(",");
            }
            let latitude = leaderA[0];
            let longitude = leaderA[1];
            page.setData({
                myleaderinfo: res.extra,
                latitude: latitude,
                longitude: longitude
            });
            //自己为团长
                        if (res.extra.iam_leader == 1) {
                let uniacid_local = wx.getStorageSync("uniacid");
                if (res.extra.uniacid && uniacid_local != res.extra.uniacid) {
                    wx.setStorage({
                        key: "changeUidCate",
                        data: 1
                    });
                    wx.setStorage({
                        key: "changeUidOrder",
                        data: 1
                    });
                    wx.setStorage({
                        key: "changeUidMy",
                        data: 1
                    });
                    wx.setStorageSync("uniacid", res.extra.uniacid);
                    console.log("此处为切换了城市为" + res.extra.uniacid + "，我是团长，直接进该城市");
                    //page.requestStart(openid);
                                        page.setData({});
                    page.onLoad();
                }
            } else {
                //有分享团长，开始判断一系列的绑定
                if (!!leader_id_share && leader_id_share != 0) {
                    const page = getCurrentPages().pop();
                    console.log(res.extra.lock_fans);
                    if (res.extra.lock_fans != 1) //开启了锁粉，不往下走
                    {
                        console.log("有分享团长");
                        //从未绑定过团长，并且自己也不是团长，并且系统没有默认绑定团长，去绑定分享的团长
                                                if (!res.extra.leader_id) {
                            console.log("准备去绑定");
                            leader.getMyLeaderInfoBind(leader_id_share);
                            //在分享团长和上次绑定团长不同的情况下  再加上本身不是团长或者未开启锁定团长
                                                } else if (leader_id_share != res.extra.leader_id && res.extra.iam_leader != 1) {
                            console.log("满足切换条件,手动切换");
                            //满足切换条件，查询需要切换的团长信息
                                                        leader.findLeaderInfo(leader_id_share);
                        }
                    }
                } else {
                    //这里是没有分享团长的情况
                    let uniacid_local = wx.getStorageSync("uniacid");
                    if (!!res.extra.uniacid && uniacid_local != res.extra.uniacid) {
                        wx.setStorage({
                            key: "changeUidCate",
                            data: 1
                        });
                        wx.setStorage({
                            key: "changeUidOrder",
                            data: 1
                        });
                        wx.setStorage({
                            key: "changeUidMy",
                            data: 1
                        });
                        wx.setStorageSync("uniacid", res.extra.uniacid);
                        console.log("此处为切换了城市为" + res.extra.uniacid + "，团长uniacid");
                        //page.requestStart(openid);
                                                page.setData({
                            imgUrls2: []
                        });
                        page.onLoad();
                    } else {
                        console.log("无分享团长");
                    }
                }
            }
        }
    });
}), 
//绑定团长（同查询团长）
//传入leader_id_share和openid
leader.getMyLeaderInfoBind = (leader_id_share => {
    var openid = wx.getStorageSync("user").openid;
    common.request({
        endpoint: "leader/myleaderinfo",
        method: "post",
        data: {
            leader_id_share: leader_id_share,
            openid: openid
        },
        success: res => {
            //console.log("没团长，绑定团长成功")
            const page = getCurrentPages().pop();
            var leaderA = [];
            if (res.extra.location) {
                leaderA = res.extra.location.split(",");
            }
            let latitude = leaderA[0];
            let longitude = leaderA[1];
            page.setData({
                myleaderinfo: res.extra,
                latitude: latitude,
                longitude: longitude
            });
            let uniacid_local = wx.getStorageSync("uniacid");
            if (uniacid_local != res.extra.uniacid) {
                wx.setStorage({
                    key: "changeUidCate",
                    data: 1
                });
                wx.setStorage({
                    key: "changeUidOrder",
                    data: 1
                });
                wx.setStorage({
                    key: "changeUidMy",
                    data: 1
                });
                if (res.extra.uniacid) {
                    wx.setStorageSync("uniacid", res.extra.uniacid);
                    console.log("此处为切换了城市为" + res.extra.uniacid + "，主动绑定团长");
                    //page.requestStart(openid);
                                        page.onLoad();
                }
            }
        }
    });
}), 
//查询需要切换的团长的信息
//并显示切换弹窗
leader.findLeaderInfo = (leader_id => {
    common.request({
        endpoint: "share/leader_info",
        data: {
            leader_id: leader_id
        },
        method: "post",
        success(res) {
            const page = getCurrentPages().pop();
            page.setData({
                currentLeaderInfo: res.extra,
                //分享的团长信息
                leaderChange: true
            });
        }
    });
}), 
/**
 * 选择团长
 */
leader.chooseButt = (e => {
    var leaderid = e.detail.id;
    const page = getCurrentPages().pop();
    var leaderName = page.data.leaderName;
    var openid = wx.getStorageSync("user").openid;
    var uniacid_local = wx.getStorageSync("uniacid");
    if (leaderid != -1) {
        common.request({
            endpoint: "leader/myleaderinfo",
            method: "post",
            data: {
                leader_id_share: leaderid,
                //此次要绑定的团长id
                openid: openid
            },
            success: res => {
                //切换团长成功，保存团长信息，隐藏弹窗
                page.setData({
                    leaderChange: false,
                    myleaderinfo: res.extra,
                    s_leader: "",
                    options: ""
                });
                wx.showToast({
                    title: "切换" + leaderName + "成功",
                    icon: "none"
                });
                //城市改变  记录新城市 全部刷新
                                if (uniacid_local != res.extra.uniacid) {
                    wx.setStorage({
                        key: "changeUidCate",
                        data: 1
                    });
                    wx.setStorage({
                        key: "changeUidOrder",
                        data: 1
                    });
                    wx.setStorage({
                        key: "changeUidMy",
                        data: 1
                    });
                    wx.setStorageSync("uniacid", res.extra.uniacid);
                    console.log("此处为切换了城市为" + res.extra.uniacid + "，切换弹窗切换团长");
                    //page.requestStart(openid);
                                        page.onLoad();
                } else {
                    //否则只刷新该团长的产品
                    page.getClass();
                }
            }
        });
    } else {
        page.setData({
            leaderChange: true,
            s_leader: "",
            options: ""
        });
    }
});

//页面leader_id异步加载问题
leader.getMyLeaderInfo_aync = ((openid, leader_id_share) => {
    return new Promise(function(resolve, reject) {
        var openid = getCurrentPages().pop().data.openid ? getCurrentPages().pop().data.openid : wx.getStorageSync("user").openid;
        //用户数据
                common.request({
            endpoint: "leader/myleaderinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                const page = getCurrentPages().pop();
                var leaderA = [];
                if (res.extra.location) {
                    leaderA = res.extra.location.split(",");
                }
                let latitude = leaderA[0];
                let longitude = leaderA[1];
                page.setData({
                    myleaderinfo: res.extra,
                    latitude: latitude,
                    longitude: longitude
                });
                resolve();
                //自己为团长
                                if (res.extra.iam_leader == 1) {
                    let uniacid_local = wx.getStorageSync("uniacid");
                    if (res.extra.uniacid && uniacid_local != res.extra.uniacid) {
                        wx.setStorage({
                            key: "changeUidCate",
                            data: 1
                        });
                        wx.setStorage({
                            key: "changeUidOrder",
                            data: 1
                        });
                        wx.setStorage({
                            key: "changeUidMy",
                            data: 1
                        });
                        wx.setStorageSync("uniacid", res.extra.uniacid);
                        console.log("此处为切换了城市为" + res.extra.uniacid + "，我是团长，直接进该城市");
                        //page.requestStart(openid);
                                                page.setData({});
                        page.onLoad();
                    }
                } else {
                    //有分享团长，开始判断一系列的绑定
                    if (!!leader_id_share && leader_id_share != 0) {
                        const page = getCurrentPages().pop();
                        console.log(res.extra.lock_fans);
                        if (res.extra.lock_fans != 1) //开启了锁粉，不往下走
                        {
                            console.log("有分享团长");
                            //从未绑定过团长，并且自己也不是团长，并且系统没有默认绑定团长，去绑定分享的团长
                                                        if (!res.extra.leader_id) {
                                console.log("准备去绑定");
                                leader.getMyLeaderInfoBind(leader_id_share);
                                //在分享团长和上次绑定团长不同的情况下  再加上本身不是团长或者未开启锁定团长
                                                        } else if (leader_id_share != res.extra.leader_id && res.extra.iam_leader != 1) {
                                console.log("满足切换条件,手动切换");
                                //满足切换条件，查询需要切换的团长信息
                                                                leader.findLeaderInfo(leader_id_share);
                            }
                        }
                    } else {
                        //这里是没有分享团长的情况
                        let uniacid_local = wx.getStorageSync("uniacid");
                        if (!!res.extra.uniacid && uniacid_local != res.extra.uniacid) {
                            wx.setStorage({
                                key: "changeUidCate",
                                data: 1
                            });
                            wx.setStorage({
                                key: "changeUidOrder",
                                data: 1
                            });
                            wx.setStorage({
                                key: "changeUidMy",
                                data: 1
                            });
                            wx.setStorageSync("uniacid", res.extra.uniacid);
                            console.log("此处为切换了城市为" + res.extra.uniacid + "，团长uniacid");
                            //page.requestStart(openid);
                                                        page.setData({
                                imgUrls2: []
                            });
                            page.onLoad();
                        } else {
                            console.log("无分享团长");
                        }
                    }
                }
            },
            fail: function(res) {
                reject(res);
            }
        });
    });
}), module.exports.leader = leader;